package com.ycsj.goldmedalnewconcept.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.flyco.dialog.listener.OnBtnLeftClickL;
import com.flyco.dialog.listener.OnBtnRightClickL;
import com.flyco.dialog.widget.NormalDialog;
import com.google.gson.Gson;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.picasso.Picasso;
import com.ycsj.goldmedalnewconcept.StartActivity;
import com.ycsj.goldmedalnewconcept.activity.GonggaoActivity;
import com.ycsj.goldmedalnewconcept.activity.MyQuanziActivity;
import com.ycsj.goldmedalnewconcept.bean.AttResponse;
import com.ycsj.goldmedalnewconcept.bean.EvaluationItem;
import com.ycsj.goldmedalnewconcept.bean.QuanziResponse;
import com.ycsj.goldmedalnewconcept.bean.HuodongResponse.Huodonglist;
import com.ycsj.goldmedalnewconcept.bean.QuanziResponse.CircleInfo;
import com.ycsj.goldmedalnewconcept.constant.Constant;
import com.ycsj.goldmedalnewconcept.utils.ListViewUtil;
import com.ycsj.goldmedalnewconcept.utils.SPUtil;
import com.ycsj.goldmedalnewconcept.utils.TimeUtil;
import com.ycsj.goldmedalnewconcept.view.CommentsAdapter;
import com.ycsj.goldmedalnewconcept.view.ImageInfo;
import com.ycsj.goldmedalnewconcept.view.NineGridView;
import com.ycsj.goldmedalnewconcept.view.NineGridViewClickAdapter;
import com.ycsj.goldmedalnewconcept.view.PicassoRoundTransform;
import com.ycsj.goldmedalnewconcept.view.PullToRefreshLayout;
import com.ycsj.goldmedalnewconcept.R;

public class HuodongAdapter extends BaseAdapter {

	private Context context;
	private List<Huodonglist> data = new ArrayList<Huodonglist>();
	private LayoutInflater mInflater;
	private String picurl;

	public void setData(List<Huodonglist> data) {
		this.data = data;
		notifyDataSetChanged();
	}

	public HuodongAdapter(Context context, List<Huodonglist> data
			) {
		this.context = context;
		this.data = data;
		mInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Huodonglist getItem(int position) {
		return (Huodonglist) data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.item_huodonglist, parent,
					false);
			convertView.setTag(new ViewHolder(convertView));
		}
		final ViewHolder holder = (ViewHolder) convertView.getTag();
		Huodonglist item = getItem(position);
		
//		Picasso.with(this).load(url).transform(new PicassoRoundTransform()).into(ivAvatar);
		holder.iv_pic =  (ImageView) convertView.findViewById(R.id.iv_pic);
		holder.tv_content = (TextView) convertView.findViewById(R.id.tv_content);
		holder.tv_tag = (TextView) convertView.findViewById(R.id.tv_tag);
		holder.tv_status = (TextView) convertView.findViewById(R.id.tv_status);
		holder.tv_address = (TextView) convertView.findViewById(R.id.tv_address);
		holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
		picurl = data.get(position).user_picture1;
		if(data.get(position).user_picture1==null||"".equals(data.get(position).user_picture1)){
			picurl = "123";
		}
		Picasso.with(context).load(picurl).error(R.drawable.huodongdefault).transform(new PicassoRoundTransform()).into(holder.iv_pic);
		holder.tv_content.setText(data.get(position).content); 			// 内容
		holder.tv_tag.setText(data.get(position).tag); 		// 标题
		holder.tv_address.setText("地点："+data.get(position).address); 		// 地址
		holder.tv_time.setText("时间："+data.get(position).begin_date+"至"+data.get(position).end_date);//时间
		
		if("0".equals(data.get(position).start_status)){
			holder.tv_status.setText("报名中");
			holder.tv_status.setBackgroundResource(R.drawable.shape_orange_huodong);
		}
		if("1".equals(data.get(position).start_status)){
			holder.tv_status.setText("进行中");
			holder.tv_status.setBackgroundResource(R.drawable.shape_yellow_btn);
		}
		if("2".equals(data.get(position).start_status)){
			holder.tv_status.setText("已结束");
			holder.tv_status.setBackgroundResource(R.drawable.shape_gary_huodong);
		}
		
		if("1".equals(data.get(position).enroll_status)){
			holder.tv_status.setText("已报名");
			holder.tv_status.setBackgroundResource(R.drawable.shape_yellow_btn);
		}
		
		
		
		
		
		
//		String time = TimeUtil
//				.getTimeFormatText(data.get(position).update_date);
//
//		holder.createTime.setText(time); // 发布时间

//		"".equals(data.get(position).user_name)
		return convertView;
	}

	class ViewHolder {
		private ImageView iv_pic;
		private TextView tv_tag;
		private TextView tv_status;
		private TextView tv_time;
		private TextView tv_address;
		private TextView tv_content;
		
		
		private View rootView;

		public ViewHolder(View convertView) {
			rootView = convertView;
			NineGridView.setImageLoader(new PicassoImageLoader());
		} 

		/** Picasso 加载 */
		private class PicassoImageLoader implements NineGridView.ImageLoader {

			@Override
			public void onDisplayImage(Context context, ImageView imageView,
					String url) {
				Picasso.with(context).load(url)//
						.placeholder(R.drawable.temp)//
						.error(R.drawable.temp)// 
						.into(imageView);
			}

			@Override
			public Bitmap getCacheImage(String url) {
				
				return null;
			}
		}

	}
}
